cd "~/Dropbox/Shared Folder - J. Ladd & M (1). Meredith/Replication Files/"
cap log close
log using "LogFiles/TableA20.log", replace

clear all
set more off

matrix Results = J(20, 10, -9)

use "RawData/GallupDataBJPS.dta", clear
// Drops Unrepresentative Series
drop if drops == "All" | regexm(drops, "Party")
// Drops Obs. w/o Gender
drop if female < 0
// Drops Obs. w/o Weights
drop if final_weight < 0
// Drops Obs. w/o Dem., Ind., or Rep.
keep if party == -1 | party == 0 | party == 1
// Drops Obs. w/o Education
drop if education < 0
// Drops Phone Surveys
keep if survey == 1

// Sets the Survey Date as the Mid-Point in Field
gen middle_date = .
replace middle_date = floor((start_date + end_date) / 2)
sort middle_date

gen year = -9
replace year = year(middle_date)  

// Codes Dem. as 0, Ind. as 1/2, and Rep. as 1
gen partisan = 0 if party == -1
replace partisan = 1 if party == 1
replace partisan = .5 if party == 0

gen yearbin = .
replace yearbin = 1 if year >= 1953 & year <= 1962
replace yearbin = 2 if year >= 1963 & year <= 1972
replace yearbin = 3 if year >= 1973 & year <= 1982
replace yearbin = 4 if year >= 1983 & year <= 1992
replace yearbin = 5 if year >= 1993 & year <= 1997
drop if missing(yearbin)

drop if age == -99 | age == -9
drop if age <= 24
gen decade_birth = floor((year - age) / 10) * 10
gen agebin = .
replace agebin = 1 if decade_birth == 1880
replace agebin = 2 if decade_birth == 1890
replace agebin = 3 if decade_birth == 1900
replace agebin = 4 if decade_birth == 1910
replace agebin = 5 if decade_birth == 1920
replace agebin = 6 if decade_birth == 1930
replace agebin = 7 if decade_birth == 1940
replace agebin = 8 if decade_birth == 1950
replace agebin = 9 if decade_birth == 1960
replace agebin = 10 if decade_birth == 1970
drop if missing(agebin)

gen college = 0
replace college = 1 if education == 5

// Estimates Jointly w/ Survey FEs

forvalues i = 1(1)5 {
local k = max(`i' - 1, 1)
local l = `i' + 5
drop if yearbin == `i' & (agebin < `k' | agebin > `l')
forvalues j = `k'(1)`l' {
gen ageXyear`j'`i'Xnc= (agebin == `j') * (yearbin == `i') * (1 - college)
gen ageXyear`j'`i'Xc = (agebin == `j') * (yearbin == `i') * college
gen ageXyear`j'`i'XncXfemale = (agebin == `j') * (yearbin == `i') * (1 - college) * female
gen ageXyear`j'`i'Xcxfemale = (agebin == `j') * (yearbin == `i') * college * female
}
drop ageXyear`k'`i'Xnc
}

areg partisan ageXyear* [pweight = final_weight], robust absorb(series) 
forvalues i = 1(1)5 {
local k = max(`i' - 1, 1)
local l = `i' + 5
forvalues j = `k'(1)`l' {
mat Results[2*(`j' - 1) + 1, `i'] = _b[ageXyear`j'`i'Xcxfemale]
mat Results[2*(`j' - 1) + 2, `i'] = -_se[ageXyear`j'`i'Xcxfemale]
mat Results[2*(`j' - 1) + 1, `i' + 5] = _b[ageXyear`j'`i'XncXfemale]
mat Results[2*(`j' - 1) + 2, `i' + 5] = -_se[ageXyear`j'`i'XncXfemale]
}
}

matlist Results

log close
